package br.com.virtual.vender.model;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import br.com.virtual.base.enums.StatusVendaEnum;
import br.com.virtual.base.listener.VirtualEntityListener;
import br.com.virtual.base.model.VirtualModel;
import br.com.virtual.cadastrar.model.Endereco;
import br.com.virtual.cadastrar.model.Pessoa;

@Entity
@EntityListeners({ VirtualEntityListener.class })
public class Venda extends VirtualModel implements Comparable<Venda> {

	private static final long serialVersionUID = 1L;

	
	@Id	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;	
	
	@ManyToOne
	private Pessoa cliente;

	@OneToMany(mappedBy = "venda", cascade = CascadeType.REFRESH, fetch = FetchType.LAZY)
	private List<ItemVenda> itens;

	@Column(name="total_frete")
	private Double totalFrete = 0.0;
	
	@Column(name="total_venda")
	private Double totalVenda = 0.0;
	
	@Column(name="total_geral")
	private Double totalGeral = 0.0;
	
	@Column(name="total_itens")
	private int totalItens = 0;

	@Column(name="data_venda") @Temporal(TemporalType.DATE)
	private Calendar dataVenda;

	@Column(name="data_entrega") @Temporal(TemporalType.DATE)
	private Calendar dataEntrega;
	
	@ManyToOne @JoinColumn(name="endereco_entrega_id")
 	private Endereco enderecoEntrega;

	@Enumerated(EnumType.ORDINAL)
	private StatusVendaEnum status;

	@OneToOne(mappedBy="venda")
	private Pagamento pagamento;


	public void calculaTotal(){
	
		Double total = 0.0;
		
		setTotalVenda(0.0);
		setTotalGeral(0.0);

		
		for(ItemVenda iv : itens){
			total += iv.getTotal();
		}
		
		setTotalVenda(total);
		setTotalGeral( total + getTotalFrete());
	}
	
	
	/*
	 * Getters And Setters
	 * */

	public Pagamento getPagamento() {
		return pagamento;
	}


	public void setPagamento(Pagamento pagamento) {
		this.pagamento = pagamento;
	}
	
	public Pessoa getCliente() {
		return cliente;
	}
	
	public void setCliente(Pessoa cliente) {
		this.cliente = cliente;
	}

	public List<ItemVenda> getItens() {

		if (itens == null)
			itens = new ArrayList<ItemVenda>();
		return itens;
	}

	public void setItens(List<ItemVenda> itens) {
		this.itens = itens;
	}

	public Calendar getDataVenda() {
		return dataVenda;
	}

	public void setDataVenda(Calendar dataVenda) {
		this.dataVenda = dataVenda;
	}

	public Endereco getEnderecoEntrega() {
		if (enderecoEntrega == null)
			enderecoEntrega = new Endereco();
		return enderecoEntrega;
	}

	public void setEnderecoEntrega(Endereco enderecoEntrega) {
		this.enderecoEntrega = enderecoEntrega;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Venda other = (Venda) obj;
		if (getId() == null) {
			if (other.getId() != null)
				return false;
		} else if (!getId().equals(other.getId()))
			return false;
		return true;
	}



	public StatusVendaEnum getStatus() {
		return status;
	}

	public void setStatus(StatusVendaEnum status) {
		this.status = status;
	}

	public Calendar getDataEntrega() {
		return dataEntrega;
	}

	public void setDataEntrega(Calendar dataEntrega) {
		this.dataEntrega = dataEntrega;
	}

	public int getTotalItens() {
		return totalItens;
	}

	public void setTotalItens(int totalItens) {
		this.totalItens = totalItens;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}


	public Double getTotalFrete() {
		return totalFrete;
	}


	public void setTotalFrete(Double totalFrete) {
		this.totalFrete = totalFrete;
	}


	public Double getTotalVenda() {
		return totalVenda;
	}


	public void setTotalVenda(Double totalVenda) {
		this.totalVenda = totalVenda;
	}


	public Double getTotalGeral() {
		return totalGeral;
	}


	public void setTotalGeral(Double totalGeral) {
		this.totalGeral = totalGeral;
	}


	@Override
	public String toString() {
		return "Venda [id=" + id + ", totalFrete=" + totalFrete
				+ ", totalVenda=" + totalVenda + ", totalGeral=" + totalGeral
				+ ", totalItens=" + totalItens + ", dataVenda=" + dataVenda
				+ ", dataEntrega=" + dataEntrega 
				+ ", status=" + status + "]";
	}


	@Override
	public int compareTo(Venda o) {

		return this.getId().compareTo(o.getId());
	}


}
